<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
<meta name="RSA" th:unless="${user.id}" th:content="${encryptionUtils.getRsaPublicKey(#request)}">
<th:block data-th-replace="/adminlte/base::base-head"></th:block>
</head>
<body class="hold-transition login-page">
	<div class="login-box">
		<div class="login-logo">
			<a href="/"><strong th:utext="${siteInfo.title}"></strong></a>
		</div>
		<div class="card">
			<div class="card-body login-card-body">
				<p class="login-box-msg">登录</p>
				<form method="post" data-encry-type="RSA" action="/logins/login" onsubmit="return login(this);" 
				data-success="loginSuccess" data-failure="loginFailure">
					<div class="input-group mb-3">
						<input id="username" type="text" name="username"
							class="form-control" placeholder="用户名" required="required"
							autofocus="autofocus">
						<div class="input-group-append">
							<div class="input-group-text">
								<span class="fas fa-user"></span>
							</div>
						</div>
					</div>
					<div class="input-group mb-3">
						<input id="password" type="password" name="password"
							class="form-control" placeholder="密码" required="required">
						<div class="input-group-append">
							<div class="input-group-text">
								<span class="fas fa-lock"></span>
							</div>
						</div>
					</div>
					<div class="row">
						<div class="col-6">
							<input id="verificationCode" type="text" name="verification"
								class="form-control"
								style="margin-left: 0; margin-right: 0; margin-bottom: 1em;"
								placeholder="验证码" maxlength="5" autocomplete="off"
								required="required">
						</div>
						<div class="col-6">
							<img id="verification" src="/verifications/num?tag=loginCode"
								class="imgcode" style="width: 100%; cursor: pointer;"
								title="点击更换验证码" onclick="refresh()">
						</div>
					</div>
					<div class="row">
						<div class="col-8">
							<div class="form-group pb-0 mb-0">
								<label>记住我</label> <select class="form-control" name="minutes">
									<option value="180">3小时</option>
									<option value="1440">1天</option>
									<option value="10080">7天</option>
									<option value="21600">半个月</option>
									<option value="43200">1个月</option>
									<option value="129600">3个月</option>
									<option value="259200">半年</option>
									<option value="525600">1年</option>
								</select>
							</div>
						</div>
						<div class="col-4 align-self-end">
							<button type="submit" class="btn btn-primary btn-block btn-flat">登录</button>
						</div>
					</div>
				</form>

				<p class="mb-1 mt-3">
					<a href="/admin/account/reset">忘了密码</a>
				</p>
				<p class="mb-0">
					<a href="/admin/account/register" class="text-center">注册新账号</a>
				</p>
			</div>
		</div>
	</div>
	
	<th:block data-th-replace="/adminlte/base::base-js"></th:block>
    <script src="/kaibes/js/base.js"></script>
    <script src="/kaibes/js/toast.js"></script>
    <script src="/kaibes/js/network.js"></script>
    <script type="text/javascript">
	    function refresh() {
	        let time = new Date().getTime();
	        document.getElementById("verification").setAttribute("src", "/verifications/num?tag=loginCode&time="+time);
	        document.getElementById("verificationCode").value = "";
	        document.getElementById("verificationCode").focus();
	    }
	    function login(obj) {
	        if(!window.localStorage){
	            alert("浏览器不支持localstorage，请换个浏览器");
	            return false;
	        }else{
	            try {
	                var aesKey = randomStr(16);
	                var aesIv = randomStr(16);
	                obj.setAttribute("data-form-aes_key", aesKey);
	                obj.setAttribute("data-form-aes_iv", aesIv);
	                localStorage["aesKey"] = aesKey;
                    localStorage["aesIv"] = aesIv;
	                formThis(obj, loginSuccess, loginFailure);
	            } catch (e) {
	                console.info(e);
	            }
	        }
	        return false;
	    }
	    function loginSuccess(status, data, headers) {
	        toast.success("登录成功，正在跳转到管理页面。。。", ()=>skip("/admin/index"));
	    }
	    function loginFailure(status, data, msg) {
	        if (data == undefined) {
	            toast.failure(msg, ()=>get("/admin/login"));
	        } else if (data.result == "WRONG_VERIFICATION") {
	            toast.failure(data.message, ()=>refresh());
	        } else if (data.result == "WRONG_PASSWORD") {
	            toast.failure(data.message, ()=>{
	                document.getElementById("password").value = "";
	                refresh();
	                document.getElementById("password").focus();
	            });
	        } else {
	            toast.failure("页面超时，请重试", ()=>skip("/admin/login"));
	        }
	    }
    </script>
</body>
</html>
